package com.example.demo.controller.admin;

import com.example.demo.entity.User;
import com.example.demo.service.admin.LoginAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;

@RequestMapping("/admin")
@Controller
public class LoginAdminController {

    @Autowired
    private LoginAdminService loginAdminService;
    private Logger logger = LoggerFactory.getLogger(LoginAdminController.class);


    /**
     * 注册用户 返回注册界面
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String Register() {
        return "register";
    }

    /**
     * 用户登录 返回登录界面
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    /**
     * 获取用户基本信息做逻辑处理
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String Login(HttpServletRequest request) {
        String userName = request.getParameter("userName");
        String passwd = request.getParameter("password");
        logger.info("UserName:" + userName + ",Passwd:" + passwd);

        User user = new User();
        user.setUserName(userName);
        user.setPassword(passwd);

        if ((!"".equals(userName)) && (!"".equals(passwd))) {
            if (loginAdminService.userLogin(user)) {
                return "home";
            } else {
                return "register";
            }
        } else {
            return "register";
        }
    }



}
